package com.example.oneSpringMVC.config;


import com.example.oneSpringMVC.interceptor.CheckPermissionInterceptor;
import com.example.oneSpringMVC.interceptor.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;


/**
 * @description:
 * @author: xinTong
 * @create: 2024-02-17 11:49
 **/
@EnableWebMvc
@Configuration
@ComponentScan(value = {"com.example.oneSpringMVC.controller", "com.example.oneSpringMVC.interceptor"})
//TODO: 进行controller扫
//WebMvcConfigurer springMvc进行组件配置的规范,配置组件,提供各种方法! 前期可以实现
public class SpringMvcConfig implements WebMvcConfigurer {
    @Autowired
    private Interceptor interceptor;
    @Autowired
    private CheckPermissionInterceptor checkPermissionInterceptor;

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
    // 添加拦截器
/*    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 1、将拦截器交给mvc管理
        registry.addInterceptor(interceptor)
                // 2、配置拦截规则
                .addPathPatterns("/**")
                // 3、配置排除规则
                .excludePathPatterns("/api/code", "/api/login", "/api/logout");

        // 1、将拦截器交给mvc管理
        registry.addInterceptor(checkPermissionInterceptor)
                // 2、配置拦截规则
                .addPathPatterns("/**")
                // 3、配置排除规则
                .excludePathPatterns("/api/code", "/api/login", "/api/logout");
    }*/

    // axios跨域访问
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
                allowCredentials(true).
                allowedHeaders("*").
                allowedOriginPatterns("*").
                allowedMethods("*");
    }
}
